package com.AX.utils;

import com.AX.entity.GeoPoint;

public class GeoUtils {
    private static final double EARTH_RADIUS = 6371e3; // 地球半径（米）

    public static double haversineDistance(GeoPoint p1, GeoPoint p2) {
        double lat1 = Math.toRadians(p1.getLat());
        double lon1 = Math.toRadians(p1.getLng());
        double lat2 = Math.toRadians(p2.getLat());
        double lon2 = Math.toRadians(p2.getLng());

        double dLat = lat2 - lat1;
        double dLon = lon2 - lon1;

        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1) * Math.cos(lat2) *
                        Math.sin(dLon/2) * Math.sin(dLon/2);

        return 2 * EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    }

    public static double calculateBearing(GeoPoint from, GeoPoint to) {
        double lat1 = Math.toRadians(from.getLat());
        double lon1 = Math.toRadians(from.getLng());
        double lat2 = Math.toRadians(to.getLat());
        double lon2 = Math.toRadians(to.getLng());

        double y = Math.sin(lon2 - lon1) * Math.cos(lat2);
        double x = Math.cos(lat1)*Math.sin(lat2) -
                Math.sin(lat1)*Math.cos(lat2)*Math.cos(lon2 - lon1);

        double bearing = Math.toDegrees(Math.atan2(y, x));
        return (bearing + 360) % 360;
    }
}
